package com.mdinfo.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.mdinfo.base.annotation.CCAuth;
import com.mdinfo.base.annotation.CCLogger;
import com.mdinfo.base.common.bean.CommonResult;
import com.mdinfo.base.common.bean.DataGrid;
import com.mdinfo.base.common.bean.Ref;
import com.mdinfo.base.utils.ListUtil;
import com.mdinfo.base.utils.TimeUtil;
import com.mdinfo.system.bean.SysMenu;
import com.mdinfo.system.bean.SysRole;
import com.mdinfo.system.bean.SysRoleMenu;
import com.mdinfo.system.mapper.SysMenuMapper;
import com.mdinfo.system.mapper.SysRoleMapper;
import com.mdinfo.system.mapper.SysRoleMenuMapper;
import com.mdinfo.system.param.RoleParam;
import com.mdinfo.system.service.IRoleService;
import com.mdinfo.system.vo.RoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2017/11/27.
 */
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Resource
    private SysRoleMapper roleMapper;
    @Resource
    private SysMenuMapper menuMapper;
    @Resource
    private SysRoleMenuMapper roleMenuMapper;

    /**
     * 添加角色
     */
    public CommonResult<Void> add(RoleParam.AddParam param){

        SysRole role = roleMapper.getByName(param.getName());
        if(role!=null){
            return Ref.error("名称已经存在");
        }
        role = new SysRole();
        role.setOrderNum(param.getOrderNum());
        role.setName(param.getName());
        role.setDescription(param.getDescription());
        role.setCreateTime(new Date());
        roleMapper.insert(role);
        return Ref.ok();
    }

    /**
     * 修改角色
     */
    public CommonResult<Void> modify(RoleParam.ModifyParam param){

        SysRole role = roleMapper.selectById(param.getRoleId());
        if(role==null){
            return Ref.error("错误的角色编号");
        }
        SysRole other = roleMapper.getByNameAndNotId(param.getName(),param.getRoleId());
        if(other!=null){
            return Ref.error("该名称已经存在");
        }
        role.setDescription(param.getDescription());
        role.setName(param.getName());
        role.setOrderNum(param.getOrderNum());
        roleMapper.updateById(role);
        return Ref.ok();
    }

    /**
     * 角色列表
     */
    @CCAuth(value = {"page-role"})
    @CCLogger("查看角色列表")
    @Transactional(readOnly = true)
    public CommonResult<DataGrid<RoleVo.ListQueryVo>> listQuery(
            RoleParam.ListQueryParam param){

        List<SysRole> roles = roleMapper.listQuery(param.getStart(), param.getNum(), param.getKey());
        int count = roleMapper.countListQuery(param.getKey());
        List<RoleVo.ListQueryVo> vos = roles.stream().map(role->{
            RoleVo.ListQueryVo vo = new RoleVo.ListQueryVo();
            vo.setCreateTime(TimeUtil.formater(role.getCreateTime()));
            vo.setDescription(role.getDescription());
            vo.setName(role.getName());
            vo.setOrderNum(role.getOrderNum());
            vo.setRoleId(role.getId());
            List<SysMenu> sysMenus = menuMapper.listByRoleId(role.getId());
            sysMenus.forEach(menu->{
                RoleVo.RoleMenuVo menuVo = new RoleVo.RoleMenuVo();
                menuVo.setMenuId(menu.getId());
                menuVo.setName(menu.getName());
                vo.getMenus().add(menuVo);
            });
            return vo;
        }).collect(Collectors.toList());
        DataGrid<RoleVo.ListQueryVo> dataGrid = new DataGrid<>();
        dataGrid.setTotal(count);
        dataGrid.setRows(vos);
        return Ref.ok(dataGrid);
    }

    /**
     * 删除角色
     */
    public CommonResult<Void> delete(RoleParam.DeleteParam param){

        SysRole role = roleMapper.selectById(param.getRoleId());
        if(role==null){
            return Ref.error("错误的角色编号");
        }
        roleMapper.deleteById(param.getRoleId());
        return Ref.ok();
    }

    /**
     * 设置权限
     */
    public CommonResult<Void> setMenu(RoleParam.SetMenuParam param){

        List<SysMenu> menus = menuMapper.listByRoleId(param.getRoleId());
        List<Integer> menuIdList = menus.stream().map(menu->{
            return menu.getId();
        }).collect(Collectors.toList());

        //获取页面参数中有,数据库中没有的  需要插入
        List<Integer> needInsertList = ListUtil.difference(param.getMenuIdList(), menuIdList);

        //获取页面参数中没有,数据库中有的  需要删除
        List<Integer> needDeleteList = ListUtil.difference(menuIdList, param.getMenuIdList());

        //删除数据
        if(needDeleteList.size()>0){
            roleMenuMapper.deleteList(param.getRoleId(),needDeleteList);
        }

        //插入数据
        needInsertList.forEach(menuId->{
            SysRoleMenu rm = new SysRoleMenu();
            rm.setMenuId(menuId);
            rm.setRoleId(param.getRoleId());
            roleMenuMapper.insert(rm);
        });
        return Ref.ok();
    }

    /**
     * 获取所有的角色
     */
    public CommonResult<List<RoleVo.ListAllVo>> listAll(){

        List<SysRole> roles = roleMapper.listAll();
        List<RoleVo.ListAllVo> vos = roles.stream().map(role->{
            RoleVo.ListAllVo vo = new RoleVo.ListAllVo();
            vo.setRoleId(role.getId());
            vo.setName(role.getName());
            return vo;
        }).collect(Collectors.toList());
        return Ref.ok(vos);
    }
}
